package com.gamificationlibrary.adapter;


import com.gamificationlibrary.gamificationlibrary.R;
import com.gamificationlibrary.model.Player;
import com.gamificationlibrary.model.leaderboard.*;


import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import android.widget.TextView;

public class LeaderboardAdapter extends ArrayAdapter<LeaderboardRow>{

	private final Player currentPlayer;
	private final Context context;
	private final Leaderboard leaderboard;

	
	public LeaderboardAdapter(Context context,Leaderboard leaderboard,Player pcurrent) 
	{
		super(context, R.layout.leaderboard_row, leaderboard.getPlayerList());
        this.context = context;
        this.leaderboard = leaderboard;
		this.currentPlayer=pcurrent;
		// TODO Auto-generated constructor stub
	}
	
	

	@Override
    public View getView(int position, View convertView, ViewGroup parent){
		
		// 1. Create inflater
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // 2. Get view from inflater

        View view = null;
        view = inflater.inflate(R.layout.leaderboard_row, parent, false);
        
	   TextView textView2 = (TextView) view.findViewById(R.id.playerNameTextView); 
	   TextView textView1 = (TextView) view.findViewById(R.id.playerRankTextView); 
	   TextView textView3 = (TextView) view.findViewById(R.id.scoreTextView); 
	   
	   LeaderboardRow lRow=leaderboard.getPlayerList().get(position);
	   textView1.setText(String.valueOf(lRow.getRank()));
	   textView2.setText(lRow.getPlayer().getIdentification());
	   textView3.setText(String.valueOf(lRow.getScore().getValue()));
	   
	   switch(position)
	   {
	   case 0:view.setBackgroundColor(Color.rgb(215, 183, 64));
	   		break;
	   case 1: view.setBackgroundColor(Color.rgb(204, 204, 204));
		break;
	   case 2: view.setBackgroundColor(Color.rgb(165, 113, 78));
		break;
  		
	   default:	break; }
	   if(textView1.getText().equals(currentPlayer.getIdentification()))
  			{
		   	textView1.setTypeface(null, Typeface.BOLD);
		   	textView2.setTypeface(null, Typeface.BOLD);
		   	textView3.setTypeface(null, Typeface.BOLD);
  			}
	   	
	   return view;
	  
    }
	
	
    }

